package pms.common.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;

import pms.common.utils.DateUtil;
import pms.common.utils.PMSLog;
import pms.common.value.ReviewValue;
import pms.common.value.ThemeValue;

public class ReviewDaoImpl implements ReviewDao {

	private JdbcTemplate jdbcTemplate;

	public JdbcTemplate getJdbcTemplate() {
		return jdbcTemplate;
	}


	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	
	public List<ReviewValue> getReviewList(int targetId) {
		String sql="select * from tbl_review where rv_target_id=? and del_fg=0 order by rv_target_id ";
		final List<ReviewValue> l_result = new ArrayList<ReviewValue>();
		jdbcTemplate.query(sql,new Object[]{new Integer(targetId)},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				ReviewValue l_tv = new ReviewValue();
				l_tv.setReviewAdmitDate(rs.getString("rv_admit_date"));
				l_tv.setReviewAdmitUserId(rs.getInt("rv_admit_user_id"));
				l_tv.setReviewCreateDate(rs.getString("rv_create_date"));
				l_tv.setReviewCycle(rs.getInt("rv_cycle"));
				l_tv.setReviewDetail(rs.getString("rv_detail"));
				l_tv.setReviewExpectDate(rs.getString("rv_expect_date"));
				l_tv.setReviewId(rs.getInt("rv_id"));
				l_tv.setReviewSolveDate(rs.getString("rv_solve_date"));
				l_tv.setReviewSolveUserId(rs.getInt("rv_solve_user_id"));
				l_tv.setReviewTargetId(rs.getInt("rv_target_id"));
				l_tv.setReviewThemeId(rs.getInt("rv_theme_id"));
				l_tv.setReviewType(rs.getInt("rv_type"));
				l_tv.setReviewUserId(rs.getInt("rv_user_id"));
				l_tv.setReviewSolveDetail(rs.getString("rv_solve_detail"));
				l_result.add(l_tv);
			}			
		});
		return l_result;
	}


	public void insertReview(ReviewValue value) {
		PMSLog.start();
		String sql="insert into tbl_review(rv_target_id,rv_theme_id,rv_create_date,rv_user_id,rv_cycle,rv_type,rv_detail,rv_solve_detail,rv_solve_user_id,rv_expect_date,rv_solve_date,rv_admit_user_id,rv_admit_date) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
		Object[] params = new Object[]{
				value.getReviewTargetId(),
				value.getReviewThemeId(),
				value.getReviewCreateDate(),
				value.getReviewUserId(),
				value.getReviewCycle(),
				value.getReviewType(),
				value.getReviewDetail(),
				value.getReviewSolveDetail(),
				value.getReviewSolveUserId(),
				value.getReviewExpectDate(),
				value.getReviewSolveDate(),
				value.getReviewAdmitUserId(),
				value.getReviewAdmitDate()				
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("theme update 0");
		} else {
			PMSLog.debug("theme updated");
		}
		PMSLog.end();
		
	}


	public void updateReview(ReviewValue value) {
		PMSLog.start();
		String sql="update tbl_review set rv_create_date=?, rv_user_id=?,rv_cycle=?,rv_type=?,rv_detail=?,rv_solve_detail=?,rv_solve_user_id=?,rv_expect_date=?,rv_solve_date=?,rv_admit_user_id=?,rv_admit_date=? where rv_target_id=? and rv_theme_id=? and rv_id=?";
		Object[] params = new Object[]{
				value.getReviewCreateDate(),
				value.getReviewUserId(),
				value.getReviewCycle(),
				value.getReviewType(),
				value.getReviewDetail(),
				value.getReviewSolveDetail(),
				value.getReviewSolveUserId(),
				value.getReviewExpectDate(),
				value.getReviewSolveDate(),
				value.getReviewAdmitUserId(),
				value.getReviewAdmitDate(),
				value.getReviewTargetId(),
				value.getReviewThemeId(),
				value.getReviewId()
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("target update 0");
		} else {
			PMSLog.debug("target updated");
		}
		PMSLog.end();
		
	}


	public void admitReview(int userId, int themeId, int targetId, int reviewId) {
		PMSLog.start();
		String sql="update tbl_review set rv_admit_user_id=?, rv_admit_date=? where rv_theme_id=? and rv_target_id=? and rv_id=?";
		Object[] params = new Object[]{
				userId,
				DateUtil.getCurrentDate(),
				themeId,
				targetId,
				reviewId
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("theme delete 0");
		} else {
			PMSLog.debug("theme deleted");
		}
		PMSLog.end();
		
	}


	public void deleteReview(int themeId, int targetId, int reviewId) {
		PMSLog.start();
		String sql="update tbl_review set del_fg=1 where rv_theme_id=? and rv_target_id=? and rv_id=?";
		Object[] params = new Object[]{
				themeId,
				targetId,
				reviewId
		};
		int result = jdbcTemplate.update(sql, params);
		if(result==0) {
			PMSLog.debug("theme delete 0");
		} else {
			PMSLog.debug("theme deleted");
		}
		PMSLog.end();
		
	}


	public List<ReviewValue> getReviewListByTheme(int themeId) {
		String sql="select * from tbl_review where rv_theme_id=? and del_fg=0 order by rv_target_id ";
		final List<ReviewValue> l_result = new ArrayList<ReviewValue>();
		jdbcTemplate.query(sql,new Object[]{new Integer(themeId)},new RowCallbackHandler(){
			public void processRow(ResultSet rs) throws SQLException {
				ReviewValue l_tv = new ReviewValue();
				l_tv.setReviewAdmitDate(rs.getString("rv_admit_date"));
				l_tv.setReviewAdmitUserId(rs.getInt("rv_admit_user_id"));
				l_tv.setReviewCreateDate(rs.getString("rv_create_date"));
				l_tv.setReviewCycle(rs.getInt("rv_cycle"));
				l_tv.setReviewDetail(rs.getString("rv_detail"));
				l_tv.setReviewExpectDate(rs.getString("rv_expect_date"));
				l_tv.setReviewId(rs.getInt("rv_id"));
				l_tv.setReviewSolveDate(rs.getString("rv_solve_date"));
				l_tv.setReviewSolveUserId(rs.getInt("rv_solve_user_id"));
				l_tv.setReviewTargetId(rs.getInt("rv_target_id"));
				l_tv.setReviewThemeId(rs.getInt("rv_theme_id"));
				l_tv.setReviewType(rs.getInt("rv_type"));
				l_tv.setReviewUserId(rs.getInt("rv_user_id"));
				l_tv.setReviewSolveDetail(rs.getString("rv_solve_detail"));
				l_result.add(l_tv);
			}			
		});
		return l_result;
	}

}
